/**
 * Classname :BaseManager.java
 *
 * Version information: 1.0
 *
 * Date: Dec 27, 2010 ho.viet.duan create
 *
 * Planex Viet Name co.ltd, All Rights Reserved.
 */
package com.zho.business.impl;

import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import com.zho.common.consumer.email.IEmailServiceHelper;
import com.zho.common.jms.JmsQueueSender;
import com.zho.common.jms.JmsTopicSender;
import com.zho.common.velocity.VelocityHelper;
import com.zho.common.velocity.VelocityTemplate;
import com.zho.dao.ICgsCustomerDAO;
import com.zho.dao.ICgsUidDAO;
import com.zho.util.Helpers;
import com.zho.util.Logit;

/**
 * 
 * @author Ho.Viet.Duan Created Jan 07, 2011
 */
public abstract class BaseManager {
	private static Logit log = Logit.getInstance(BaseManager.class);

	private IEmailServiceHelper emailServiceHelper;
	private JmsQueueSender jmsQueueSender;
	private JmsTopicSender jmsTopicSender;

	// Start DAO
	private ICgsCustomerDAO cgsCustomerDAO;
	private ICgsUidDAO cgsUidDAO;

	// End DAO

	/**
	 * Encode
	 * 
	 * @param
	 * @return
	 * @auth ho.viet.duan
	 * @CrDate Dec 07, 2011
	 * @MdDate
	 */
	public String encodeString(String str) {
		Md5PasswordEncoder encode = new Md5PasswordEncoder();
		return encode.encodePassword(str, null);
	}

	public void sendMail(final String to, final String subject,
			String template, Map<String, Object> params) throws Exception {

		// Get template
		String pathFile = Helpers.getFullPath(template);
		final String content = VelocityHelper.execute(
				VelocityTemplate.getTemplateContent(pathFile, false), params);

		// Send mail
		getEmailServiceHelper().addEmailMsg(new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws Exception {
				MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
						true, "utf-8");
				message.setFrom("email@email");
				message.setTo(to);
				message.setSubject(subject);
				message.setText(content, true);
			}
		});
	}

	// Start: Generate Get/Set methods
	public IEmailServiceHelper getEmailServiceHelper() {
		return emailServiceHelper;
	}

	public void setEmailServiceHelper(IEmailServiceHelper emailServiceHelper) {
		this.emailServiceHelper = emailServiceHelper;
	}

	public ICgsCustomerDAO getCgsCustomerDAO() {
		return cgsCustomerDAO;
	}

	public void setCgsCustomerDAO(ICgsCustomerDAO cgsCustomerDAO) {
		this.cgsCustomerDAO = cgsCustomerDAO;
	}

	public ICgsUidDAO getCgsUidDAO() {
		return cgsUidDAO;
	}

	public void setCgsUidDAO(ICgsUidDAO cgsUidDAO) {
		this.cgsUidDAO = cgsUidDAO;
	}

	public JmsQueueSender getJmsQueueSender() {
		return jmsQueueSender;
	}

	public void setJmsQueueSender(JmsQueueSender jmsQueueSender) {
		this.jmsQueueSender = jmsQueueSender;
	}

	public JmsTopicSender getJmsTopicSender() {
		return jmsTopicSender;
	}

	public void setJmsTopicSender(JmsTopicSender jmsTopicSender) {
		this.jmsTopicSender = jmsTopicSender;
	}
	// End: Generate Get/Set methods
}
